<?php
namespace app\home\controller;
use app\home\controller;
use app\home\model\PatrolDeviceM;
use myResponse\Res;
use think\Db;
use think\Request;

//设备分类控制器
class PatrolDevice extends Base{
    //显示分类列表

    function getLineDevice(){

        $m= new PatrolDeviceM;
        $z=$m->listP();
        Res::suc('ok',$z);
    }

    //添加设备分类
    function addOne(){
        $m= new PatrolDeviceM;
        $this->ret($m->addOne(),0);
    }
    //修改
    function editOne(){
        $m= new PatrolDeviceM;
        $this->ret($m->editOne(),1);
    }
    //删除
    function delOne(){
        $pdid=input('PatrolDeviceID');
        $this->delItemTask($pdid);
        $m= new PatrolDeviceM;
        $this->ret($m->delOne(),2);
    }

    function ret($z,$tp){
        $ret=['添加','修改','删除'];
        if($z)
            Res::suc($ret[$tp].'成功！');
        else
            Res::errs($ret[$tp].'失败！');
    }
    //重置巡检任务
    function resetOne(){
        $pdid=input('PatrolDeviceID');
        $this->delItemTask($pdid);
        // 检查触发时间重置
        $time = date('Y-m-d H:i:s',time());
        Db::execute("update patrol_device set nearTime='{$time}' where PatrolDeviceID=$pdid");
        Res::suc('重置成功,巡检任务将按新时间生成！');
    }
    function delItemTask($pdid){
        $one = Db::table('patrol_device')->find($pdid);
        $map['LineID'] = $one['LineID'];
        $map['sbid'] = $one['sbid'];
        Db::table('patrol_device_item_task')->where($map)->delete();
    }
}
